package com.example.fashionshoes.task;

import com.example.fashionshoes.constant.OrderConst;
import com.example.fashionshoes.pojo.po.Order;
import com.example.fashionshoes.service.OrderService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class OrderTask {
    @Resource
    private OrderService orderService;
    /**
     * 每两小时查看是否有订单超时小时，并设置订单状态
     */
    @Scheduled(fixedRate = 2 * 60 * 60 * 1000)
    public void checkOrder(){
        log.info("正在清除超时订单！当前时间为：{}",LocalDateTime.now());
        List<Order> timeoutOrders  = orderService.lambdaQuery().lt(Order::getOrderTime, LocalDateTime.now().minusHours(2)).eq(Order::getStatus, OrderConst.UN_PAY).list();
        if (timeoutOrders.isEmpty()) {
            log.info("当前没有超时订单");
            return;
        }
        log.info("超时订单有{}个",timeoutOrders.size());
        timeoutOrders.forEach(order -> order.setStatus(OrderConst.TIMEOUT));
        orderService.updateBatchById(timeoutOrders);
    }
}
